/*
 * @ProjectName: flutter_common
 * @Package: 
 * @ClassName: country_list.dart
 * @Description: 国家列表
 * @User: Page.Su
 * @Date: 2020/8/28
 * @Time: 12:01
 * @UpdateDate: 2020/8/28 12:01
 */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_su/common/app_color.dart';
import 'package:flutter_su/common/application.dart';
import 'package:flutter_su/common/fun.dart';
import 'package:flutter_su/common/language_text.dart';
import 'package:flutter_su/enum/language_type.dart';
import 'package:flutter_su/model/country_list_model.dart' as countryListModel;
import 'package:flutter_su/provider/language_provider.dart';
import 'package:flutter_su/services/audit_api.dart';
import 'package:provider/provider.dart';

class CountryList extends StatefulWidget {

  final String countryCode;

  const CountryList({Key key, this.countryCode = ""}) : super(key: key);

  @override
  _CountryListState createState() => _CountryListState();
}

class _CountryListState extends State<CountryList> {
  LanguageProvider _languageProvider; // app语言的状态管理

  @override
  void didChangeDependencies() {
    _languageProvider = Provider.of<LanguageProvider>(context, listen: false);
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: LanguageText.countryList,
        centerTitle: true,
        elevation: 0,
        backgroundColor: AppColor.MAIN_COLOR_BLUE,
      ),
      backgroundColor: AppColor.MAIN_COLOR_BLUE,
      body: _body(),
    );
  }

  Widget _body() {
    return Container(
      child: FutureBuilder<countryListModel.CountryListModel>(
        future: _getCountryList(),
        builder: (BuildContext context,
            AsyncSnapshot<countryListModel.CountryListModel> snapshot) {
          return _futureWidget(snapshot);
        },
      ),
    );
  }

  Widget _futureWidget(
      AsyncSnapshot<countryListModel.CountryListModel> snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.data != null) {
        return _list(snapshot.data);
      } else {
        return _errorWidget();
      }
    }
    return Center(
      child: SpinKitFadingCircle(
        color: Colors.black,
        size: SFunCommon.width(90),
      ),
    );
  }

  Widget _errorWidget() {
    return Center(
      child: RaisedButton(
        color: AppColor.MAIN_COLOR_CYAN,
        child: LanguageText.retry,
        onPressed: () {
          setState(() {});
        },
      ),
    );
  }

  Future<countryListModel.CountryListModel> _getCountryList() async {
    return AuditApi().getCountryList();
  }

  Widget _list(countryListModel.CountryListModel model) {
    int itemCount = model.data.length;
    return Container(
      child: ListView.separated(
        itemCount: itemCount,
        itemBuilder: (BuildContext context, int index) {
          countryListModel.Data itemData = model.data[index];
          if(index == itemCount -1) {
            return _item(itemData, isEndItem: true);
          }
          return _item(itemData);
        },
        separatorBuilder: (context, index) {
          return Divider(
            indent: SFunCommon.width(75),
            endIndent: SFunCommon.width(75),
            color: AppColor.MAIN_COLOR_CYAN,
          );
        },
      ),
    );
  }

  Widget _item(countryListModel.Data itemData, {bool isEndItem = false}) {
    Color textColor = itemData.countryCode == widget.countryCode ? Colors.red : Colors.white;
    Widget text = _languageProvider.getLanguageType == LanguageType.chinese
        ? Text(
            itemData.countryZhname,
            style: SFunCommon.fontStyle(
              textColor,
              fontSize: 30,
            ),
          )
        : Text(
            itemData.countryEnname,
            style: SFunCommon.fontStyle(
              textColor,
              fontSize: 30,
            ),
          );

    return GestureDetector(
      onTap: () {
        if (_languageProvider.getLanguageType == LanguageType.chinese) {
          String data = itemData.countryZhname + "/${itemData.countryCode}";
          Navigator.pop<String>(context, data);
        } else {
          String data = itemData.countryEnname + "/${itemData.countryCode}";
          Navigator.pop<String>(context, data);
        }
      },
      child: Container(
        color: AppColor.TRANSPARENT,
        margin: EdgeInsets.only(
          bottom: isEndItem ? SFunCommon.height(50) : 0,
          left: SFunCommon.width(80),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            text,
          ],
        ),
      ),
    );
  }
}
